package com.yxx.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.yxx.entity.Friend;
import com.yxx.entityModel.vo.Result.PageResult;
import com.yxx.entityModel.vo.query.FriendQuery;
import com.yxx.entityModel.vo.request.FriendRequest;
import com.yxx.entityModel.vo.response.FriendBackResponse;
import com.yxx.entityModel.vo.response.FriendResponse;
import com.yxx.mapper.FriendMapper;
import com.yxx.service.FriendService;
import com.yxx.utils.BeanCopyUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

/**
 * @author yuxiangxun
 * @date 2025/1/31
 * @apiNote
 */
@Service
@Slf4j
public class FriendServiceImpl extends ServiceImpl<FriendMapper,Friend> implements FriendService   {

    @Resource
    private FriendMapper friendMapper;

    /**
     * 查看友链链表
     * @return
     */
    @Override
    public List<FriendResponse> friendListVO() {
        return friendMapper.selectFriendListVO();
    }

    /**
     * 查看后台友链列表
     * @param friendQuery
     * @return
     */
    @Override
    public PageResult<FriendBackResponse> friendListBackVO(FriendQuery friendQuery) {
        // 查询友链数量
        Long count = friendMapper.selectCount(new LambdaQueryWrapper<Friend>().like(StringUtils.hasText(friendQuery.getKeyword()), Friend::getName, friendQuery.getKeyword()));
        // 如果友链数量为0，则new一个分页结果对象并返回
        if(count == 0){
            return new PageResult<>();
        }
        // 如果友链数量不为0，则查询友链后台信息并返回
        List<FriendBackResponse> friendBackResponsesList = friendMapper.selectFriendBackListVO(friendQuery);
        return new PageResult<>(friendBackResponsesList,count);
    }

    /**
     * 添加友链
     * @param friendRequest
     */
    @Override
    public void addFriend(FriendRequest friendRequest) {
        // 构建友链对象 这里用的是对象转换
        Friend newFriend = BeanCopyUtils.copyBean(friendRequest, Friend.class);
        // 添加友链
        baseMapper.insert(newFriend);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean removeByIds(Collection<?> friendIdList) {
        if (CollectionUtils.isEmpty(friendIdList)) {
            return false;
        } else {
            TableInfo tableInfo = TableInfoHelper.getTableInfo(this.getEntityClass());
            return tableInfo.isWithLogicDelete() && tableInfo.isWithUpdateFill() ? this.removeBatchByIds(friendIdList, true) : SqlHelper.retBool(this.getBaseMapper().deleteBatchIds(friendIdList));
        }
    }

    /**
     * 修改友链
     * @param friendRequest
     */
    @Override
    public void updateFriend(FriendRequest friendRequest) {
        // 新友链  对象转换
        Friend newFriend = BeanCopyUtils.copyBean(friendRequest,Friend.class);
        // 更新友链
        baseMapper.updateById(newFriend);
    }
}
